﻿using System;
using System.Windows;
using System.Windows.Media.Animation;
using GalaSoft.MvvmLight.Messaging;
using HP.Cachalote.WPF.ViewModel;
using HP.Cachalote.Services;
using HP.Cachalote.WPF.UserControls;
using HP.Cachalote.WPF.Shared;

namespace HP.Cachalote.WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : WindowBase
    {
        private bool _showLeftPanel = true;

        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            Closing += (s, e) => ViewModelLocator.Cleanup();
        }

        protected override void RegisterMessages()
        {
            base.RegisterMessages();

            RegisterDisplayEditNoteWindowMessage();
            RegisterRequestDisplayDueDateTimePickerWindowMessage();
        }

        /// <summary>
        /// Register RequestDisplayEditNoteWindowMessag from TodoItemTreeViewItemViewModel then open EditNoteWindow on the view.
        /// </summary>
        private void RegisterDisplayEditNoteWindowMessage()
        {
            Messenger.Default.Register<EditNoteViewModel>(this, TodoItemTreeViewItemViewModel.RequestDisplayEditNoteWindowMessageToken, viewModel =>
            {
                var editNoteWindow = new EditNoteWindow();
                editNoteWindow.Owner = this;
                editNoteWindow.DataContext = viewModel;
                editNoteWindow.ShowDialog();
            });
        }

        /// <summary>
        /// Register RequestDisplayDueDateTimePickerWindowMessage from TodoItemTreeViewItemViewModel then open DueDateTimeWindow on the view.
        /// </summary>
        private void RegisterRequestDisplayDueDateTimePickerWindowMessage()
        {
            Messenger.Default.Register<DueTimePickerViewModel>(this, TodoItemTreeViewItemViewModel.RequestDisplayDueDateTimePickerWindowMessageToken, viewModel =>
            {
                var dueTimePickerWindow = new DueTimePickerWindow();
                dueTimePickerWindow.Owner = this;
                dueTimePickerWindow.DataContext = viewModel;
                dueTimePickerWindow.ShowDialog();
            });
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void BtnRestore_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Normal;
        }

        private void BtnMinimize_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void BtnMaximize_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Maximized;
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            BtnRestore.Visibility = WindowState == WindowState.Maximized ? Visibility.Visible : Visibility.Hidden;
            BtnMaximize.Visibility = WindowState == WindowState.Maximized ? Visibility.Hidden : Visibility.Visible;
        }

        private void leftPanelVisibilityControlButton_Click(object sender, RoutedEventArgs e)
        {
            // If left panel is hidden then plan StoryboardShowLeftPanel, otherwize play StoryboardHideLeftPanel.
            var storyboard = _showLeftPanel ? FindResource("StoryboardHideLeftPanel") as Storyboard : FindResource("StoryboardShowLeftPanel") as Storyboard;
            _showLeftPanel = !_showLeftPanel;
            storyboard.Begin();
        }

        private void BtnAbout_Click(object sender, RoutedEventArgs e)
        {
            var aboutWindow = new AboutWindow();
            aboutWindow.Owner = this;
            aboutWindow.ShowDialog();
        }
    }
}